package com.qc.wintrax.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qc.wintrax.R;
import com.qc.wintrax.activity.EventInfoActivity;
import com.qc.wintrax.adapter.EventInfoAdapter;
import com.qc.wintrax.model.EventInfo;
import com.qc.wintrax.utils.IntentUtil;
import com.qc.wintrax.utils.LogUtil;
import com.qc.wintrax.utils.ToastUtil;
import com.qc.wintrax.widget.SearchEditView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.R.attr.editable;
import static com.loc.e.e;
import static com.loc.e.r;

/**
 * @author xiaobaodou
 * @Description:(活动页面) create at  2016/7/14 17:00
 **/
public class EventFragment extends BaseFragment {


    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.edit_search)
    SearchEditView editSearch;

    private EventInfoAdapter eventInfoAdapter;
    private List<EventInfo> infoList;
    private List<EventInfo> newinfoList;
    private Map<String, EventInfo> map;
    private ArrayList<String> strlist;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab01, container, false);
        ButterKnife.bind(this, view);
        resetColor();
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void setValues() {
        if (newinfoList != null && newinfoList.size()>0) {
            newinfoList.clear();
        }
        infoList = new ArrayList<EventInfo>();
        for (int i = 0; i < 3; i++) {
            EventInfo info = new EventInfo(R.mipmap.ceshi, "2016青浦练塘古镇跑", "发布者:上海求创科技", "发布时间:2016-08-22", "qpltgzp");
            EventInfo info1 = new EventInfo(R.mipmap.ceshi, "2018南北湖马拉松", "发布者:上海求创科技", "发布时间:2016-08-21", "nbhmls");
            EventInfo info2 = new EventInfo(R.mipmap.ceshi, "2016桃花谷比赛", "发布者:上海求创科技", "发布时间:2016-08-20", "thgbs");
            infoList.add(info);
            infoList.add(info1);
            infoList.add(info2);
        }
        map = new HashMap<String, EventInfo>();
        strlist = new ArrayList<String>();
        for (int j = 0; j < infoList.size(); j++) {
            EventInfo eventInfo = infoList.get(j);
            String name = eventInfo.getTitle();
            strlist.add(name);
            map.put(name, eventInfo);
        }
        LogUtil.e("huuoefpd f---" + strlist.size());
        eventInfoAdapter = new EventInfoAdapter(getActivity(), infoList, R.layout.event_item);
        listView.setAdapter(eventInfoAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String title = infoList.get(position).getTitle();
                IntentUtil.startActivity(EventInfoActivity.class,new Intent().putExtra("titles",title));
            }
        });
    }

    public void textChange(){
        newinfoList = new ArrayList<EventInfo>();
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    for (int a = 0; a < strlist.size(); a++) {
                        if (strlist.get(a).contains(editable.toString())) {
                            String key = strlist.get(a);
                            EventInfo eventInfo = map.get(key);
                            LogUtil.e("想要获取的对象是---" + eventInfo);
                            newinfoList.add(eventInfo);
                        }
                    }
                    if (newinfoList.size() > 0) {
                        eventInfoAdapter.updateList(newinfoList);
                        infoList.clear();
                        eventInfoAdapter.notifyDataSetChanged();
                        eventInfoAdapter = new EventInfoAdapter(getActivity(), newinfoList, R.layout.event_item);
                        listView.setAdapter(eventInfoAdapter);
                        eventInfoAdapter.notifyDataSetChanged();
                    }else {
                        ToastUtil.show(getActivity(),"没有与之匹配的结果！");
                    }
                } else {
                    newinfoList.clear();
                    infoList.clear();
                    strlist.clear();
                    map.clear();
                    eventInfoAdapter.notifyDataSetChanged();
                    setValues();
                }
            }
        });
    }
    public void resetColor() {
        setValues();
        textChange();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
